﻿using MINERP.BLL.CustomerManage;
using MINERP.BLL.NewPhotoRindGrid;
using MINERP.COM;
using MINERP.ComControls;
using MINERP.MODEL.FoundationModel;
using MINERP.MODEL.NewProject;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MINERP.ComControls.Extension;

namespace MINERP.FactoryMerger
{
    public partial class FrmFatoryMerger : FrmParent
    {
        OrderInfoBLL _orderbll = new OrderInfoBLL();
        public FrmFatoryMerger()
        {
            InitializeComponent();
        }
        GridBLL_Grid gridBll = new GridBLL_Grid();

        public List<string> listCode = new List<string>();

        public void InitialOutShopGrid()
        {
            foreach (Grid_Info grid in listgrid)
            {
                OutScan outscan = new OutScan();
                outscan.Tag = grid;
                outscan.Width = 200;
                outscan.Height = 150;
                outscan.gridname = grid.GridName;
                outscan.existence = false;
                outscan.Click += outscan_Click;
                if (grid.recordModel != null)
                {

                    if (grid.recordModel.orderinfoModel != null)
                    {
                        outscan.existence = true;
                        outscan.shopName = grid.recordModel.orderinfoModel.ShopName;
                        outscan.ctmName = grid.recordModel.orderinfoModel.CtmName;
                        outscan.orderName = grid.recordModel.orderinfoModel.OrderProductName;
                        outscan.orderCode = grid.recordModel.orderinfoModel.OrderProductCode;
                        outscan.Num = grid.recordModel.Num;

                        listCode.Add(outscan.orderCode);
                    }
                }
                tableLayoutPanelOutOrder.Controls.Add(outscan);

            }
        }

        void outscan_Click(object sender, EventArgs e)
        {
            OutScan os = sender as OutScan;
            Grid_Info grid = os.Tag as Grid_Info;
            os.BackColor = Color.Red;

            foreach (Control item in tableLayoutPanelOutOrder.Controls)
            {

                OutScan outscan = item as OutScan;
                Grid_Info grinfo = outscan.Tag as Grid_Info;
                if (grid.GridId != grinfo.GridId)
                {
                    outscan.BackColor = Color.PeachPuff;
                }
            }
        }
        List<Grid_Info> listgrid = new List<Grid_Info>();
        private void FrmNewProject_Load(object sender, EventArgs e)
        {
           cbmDisType.Text="产品条码";
            //   cbmDisType.SelectedIndex = 0;

            listgrid = gridBll.GetGridInfoList();
            InitialOutShopGrid();
        }
        //确定
        private void BtnQD_Click(object sender, EventArgs e)
        {
            SaoMiao();
        }
        //清理格子
        private void BtnQL_Click(object sender, EventArgs e)
        {

            if (DialogResult.OK == MessageBox.Show("确认清理该格子", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {

                int gridid = 0;
                OutScan outsc = new OutScan();
                foreach (Control item in tableLayoutPanelOutOrder.Controls)
                {

                    OutScan outscan = item as OutScan;
                    Grid_Info grinfo = outscan.Tag as Grid_Info;

                    if (outscan.BackColor == Color.Red)
                    {
                        outsc = outscan;
                        gridid = grinfo.GridId;
                        break;
                    }
                }
                string mes = "";

                bool isgrid = gridBll.ClearGridData(gridid, out mes);
                if (isgrid == true)
                {
                    MessageBox.Show("格子清楚成功！");
                    outsc.RefreshQC();

                }
                else
                {
                    MessageBox.Show("格子清楚失败！" + mes);
                }
            }
        }

        private void txtCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SaoMiao();
            }
        }

        public void SaoMiao()
        {
            string code = txtCode.Text.Trim();
            if (code.Trim().Length == 0)
            {
                return;
            }
            if (code.Length < 10)
            {


                if (cbmDisType.Text == "产品条码")
                {
                    if (code.Length != 10)
                    {
                        code = "2" + string.Format("{0:d9}", Convert.ToInt32(code));
                    }

                }
                else
                {
                    code = "3" + string.Format("{0:d9}", Convert.ToInt32(code));
                }
            }
            else if (code.Length > 10)
            {
                MessageBox.Show("条码长度超过10位，请检验！");
                return;
            }
            if (code.Length == 10) 
            {
              string str = code.Substring(0, 1);
              if (str == "2" && cbmDisType.Text == "照片条码") 
              {
                  MessageBox.Show("该条码是产品条码，系统会自动更改类型！");
                  cbmDisType.Text = "产品条码";
                  
              }
              else if (str == "3" && cbmDisType.Text == "产品条码")
              {
                  MessageBox.Show("该条码是照片条码，系统会自动更改类型！");
                  cbmDisType.Text = "照片条码";
              }
            }
                int gzid = 0;
                OrderInfoProject listodinfo = _orderbll.ProjectSM(code, base.userId, out gzid);
                if (listodinfo.orderCode == null)
                {
                    MessageBox.Show("该条码还未进入合册流程！");//不存在或
                    return;
                }
                else
                {      
                    if (listCode.Contains(code))
                    {
                        foreach (Control item in tableLayoutPanelOutOrder.Controls)
                        {

                            OutScan outscan = item as OutScan;
                            Grid_Info grinfo = outscan.Tag as Grid_Info;
                            if (outscan.existence && grinfo.GridId == gzid)
                            {
                                outscan.Num = listodinfo.Num;
                                outscan.RefreshNum();
                                break;
                            }
                        }
                    }
                    else
                    {

                        foreach (Control item in tableLayoutPanelOutOrder.Controls)
                        {
                            OutScan outscan = item as OutScan;
                            Grid_Info grinfo = outscan.Tag as Grid_Info;
                            if (grinfo.GridId == gzid)
                            {
                                outscan.existence = true;

                                outscan.shopName = listodinfo.shopName;
                                outscan.ctmName = listodinfo.CtmName;
                                outscan.orderName = listodinfo.orderName;
                                outscan.orderCode = listodinfo.orderCode;
                                outscan.Num = listodinfo.Num;
                                outscan.RefreshNum();
                                break;
                            }
                        }
                    }

                    foreach (Control item in tableLayoutPanelOutOrder.Controls)
                    {
                        OutScan outscan = item as OutScan;
                        Grid_Info grinfo = outscan.Tag as Grid_Info;
                        if (gzid != grinfo.GridId)
                        {
                            outscan.BackColor = Color.PeachPuff;
                        }
                        else
                        {
                            outscan.BackColor = Color.Red;
                        }
                    }
                }

           

           
        }
        //完成
        private void BtnWC_Click(object sender, EventArgs e)
        {

        }
    }
}
